package core

import (
	"framework-server/global"
	"github.com/sirupsen/logrus"
	rotatelogs "github.com/lestrrat-go/file-rotatelogs"
	"time"
)

func ApiLog() (logger *logrus.Logger) {

	logger = logrus.New()
	filePath := global.GVA_CONFIG.ApiLog.FileName

	writer, err := rotatelogs.New(
		filePath+".%Y%m%d",
		rotatelogs.WithLinkName(filePath),         // 生成软链，指向最新日志文件
		rotatelogs.WithMaxAge(time.Second*86400*7),     // 文件最大保存时间
		rotatelogs.WithRotationTime(time.Second*86400), // 日志切割时间间隔
	)
	if err != nil {
		logrus.Fatal("Init log failed, err:", err)
	}
	//logrus.SetOutput(writer)
	logger.Out = writer

	//logrus.SetFormatter(&logrus.JSONFormatter{
	//	TimestampFormat:"2006-01-02 15:04:05",
	//})
	logger.Formatter = &logrus.JSONFormatter{
		TimestampFormat: "2006-01-02 15:04:05",
	}
	switch global.GVA_CONFIG.ApiLog.Level {
	case "info":
		logger.Level = logrus.InfoLevel
	case "debug":
		logger.Level = logrus.DebugLevel
	case "warn":
		logger.Level = logrus.WarnLevel
	default:
		logrus.Fatal("log conf only allow [info, debug, warn], please check your confguire")
	}
	return
}